/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.medbeans.modules.search;


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JPanel;

import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerPanel;
import org.openide.explorer.view.ListView;
import org.openide.nodes.Node;
import org.openide.util.actions.SystemAction;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;


/**
 * Panel showing details. Used in <code>ResultViewTopComponent</code>.
 *
 * @author  Peter Zavadsky
 */
public class DetailsPanel extends JPanel {

    /** Explorer panel showing detail in explorer panel list view. */
    private ExplorerPanel detailExplorerPanel;
    
    
    /** Creates new form DetailsPanel */
    public DetailsPanel(final TopComponent topComponent) {
        initComponents();
        
        detailExplorerPanel = new ExplorerPanel();
        detailExplorerPanel.setLayout(new BorderLayout());
        
        detailExplorerPanel.getExplorerManager().addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if(ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
                    Node[] selectedNodes = detailExplorerPanel.getExplorerManager().getSelectedNodes();
                    topComponent.setActivatedNodes(selectedNodes);

                    // Tricky code for performing second action on detail node
                    // when the node is selected.
                    if(selectedNodes == null || selectedNodes.length == 0)
                        return;
                    
                    SystemAction[] actions = selectedNodes[0].getActions();
                    
                    if(actions == null || actions.length < 2)
                        return;
                    
                    SystemAction action = actions[1];
                    if(action != null)
                        action.actionPerformed(new ActionEvent(selectedNodes[0], 0, null));
                }
            }
        });
        
        ListView listView = new ListView();
	listView.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(DetailsPanel.class).getString("ACS_ListView"));
        detailExplorerPanel.add(listView, BorderLayout.CENTER);
        
        detPanel.add(detailExplorerPanel, BorderLayout.CENTER);        
        positionLabel.setLabelFor(listView);                
    }

   
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        positionLabel = new javax.swing.JLabel();
        locationInfoPanel = new org.medbeans.modules.search.LocationInfoPanel();
        detPanel = new javax.swing.JPanel();

        setLayout(new java.awt.GridBagLayout());

        setBorder(new javax.swing.border.TitledBorder(""));
        positionLabel.setText(NbBundle.getBundle(DetailsPanel.class).getString("TEXT_LABEL_POSITION"));
        positionLabel.setDisplayedMnemonic(NbBundle.getBundle(DetailsPanel.class).getString("TEXT_LABEL_POSITION_MNEM").charAt(0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(17, 11, 0, 11);
        add(positionLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(11, 11, 0, 11);
        add(locationInfoPanel, gridBagConstraints);

        detPanel.setLayout(new java.awt.BorderLayout());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(7, 11, 11, 11);
        add(detPanel, gridBagConstraints);

    }//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel positionLabel;
    private org.medbeans.modules.search.LocationInfoPanel locationInfoPanel;
    private javax.swing.JPanel detPanel;
    // End of variables declaration//GEN-END:variables

    /** Sets detail nodes via its root. */
    void setDetailsRoot(Node detailRoot) {
        detailExplorerPanel.getExplorerManager().setRootContext(detailRoot);
    }
    
    /** Delegates call to location info panel. */
    void showInfo(Node node) {
        locationInfoPanel.showInfo(node);
    }
    
}
